/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ivanbiz.siswa;

import com.ivanbiz.departement.Departement;
import com.ivanbiz.departement.DepartementDAO;
import com.ivanbiz.perpustakaan.KartuPerpus;
import com.ivanbiz.perpustakaan.KartuPerpusDAO;
import com.ivanbiz.tools.MD5;
import com.ivanbiz.tools.RandomList;
import com.ivanbiz.user.User;
import com.ivanbiz.user.UserDAO;
import java.util.List;

/**
 *
 * @author User
 */
public class SiswaServiceImpl implements SiswaService {

    private SiswaDAO siswaDAO;
    private KartuPerpusDAO kartuPerpusDAO;
    private UserDAO userDAO;
    private DepartementDAO departementDAO;

    public void setSiswaDAO(SiswaDAO siswaDAO) {
        this.siswaDAO = siswaDAO;
    }

    public void setKartuPerpusDAO(KartuPerpusDAO kartuPerpusDAO) {
        this.kartuPerpusDAO = kartuPerpusDAO;
    }
    
    public void setUserDAO(UserDAO userDAO) {
        this.userDAO = userDAO;
    }

    public void setDepartementDAO(DepartementDAO departementDAO) {
        this.departementDAO = departementDAO;
    }

    public void saveSiswa(Siswa siswa) throws Exception {
        boolean kodeSiswaIsUsed = cekIsEmptyKode(siswa.getKode());
        if (!kodeSiswaIsUsed) {
            for (int x = 0; x < siswa.getSiswaCard().size(); x++) {
                SiswaCard siswaCard = (SiswaCard) siswa.getSiswaCard().get(x);
                boolean isEmpty = this.siswaDAO.cekIsEmptyCard(siswaCard.getNoCard(), siswaCard.getNama());
                if (!isEmpty) {
                    if (siswaCard.getNama().equals("PERPUSTAKAAN")) {
                        KartuPerpus kartuPerpus = new KartuPerpus();
                        kartuPerpus.setKode(siswaCard.getNoCard());
                        kartuPerpus.setKodeAccount(siswa.getKode());
                        this.kartuPerpusDAO.save(kartuPerpus);
                        break;
                    }
                } else {
                    Double.parseDouble("throws error");
                }
            }
            User user = new User();
            user.setUserId(siswa.getKode());
            user.setPassword("password");
            user.setNama(siswa.getNama());
            RandomList rl = new RandomList();
            //user.setToken(new MD5(rl.getGenerateKey()).toString());
            Departement departement = this.departementDAO.getByNamaBagian("SISWA");
            user.setDepartement(departement);
            this.userDAO.save(user);
            this.userDAO.flushUser();
            siswa.setUser(user);
            siswa.setToken(new MD5(rl.getGenerateKey()).toString());
            this.siswaDAO.save(siswa);
        } else {
            Double.parseDouble("Kode Sudah Digunakan");            
        }
    }

    public void deleteSiswa(Siswa siswa) throws Exception {
        this.siswaDAO.delete(siswa);
    }

    public void updateSiswa(Siswa siswa) throws Exception {
        System.out.println(siswa.getSiswaCard());
        for (int x = 0; x < siswa.getSiswaCard().size(); x++) {
            SiswaCard siswaCard = (SiswaCard) siswa.getSiswaCard().get(x);
            // boolean isEmpty = this.siswaDAO.cekIsEmptyCard(siswaCard.getNoCard(), siswaCard.getNama());
            //if (!isEmpty) {
            if (siswaCard.getNama().equals("PERPUSTAKAAN")) {
                KartuPerpus kartuPerpus = (KartuPerpus) this.kartuPerpusDAO.getDataByEqual(KartuPerpus.class, "kodeAccount", siswaCard.getSiswa().getKode());
                kartuPerpus.setKode(siswaCard.getNoCard());
                kartuPerpus.setKodeAccount(siswa.getKode());
                this.kartuPerpusDAO.update(kartuPerpus);
                break;
            }
        // } else {
        //     Double.parseDouble("throws error");
        // }
        }
        this.siswaDAO.update(siswa);
    }

    public List loadAll() throws Exception {
        return this.siswaDAO.getAll(Siswa.class);
    }

    public Siswa loadById(long id) throws Exception {
        return (Siswa) this.siswaDAO.load(Siswa.class, id);
    }

    public boolean cekIsEmptyCard(String kode, String nama) throws Exception {
        return this.siswaDAO.cekIsEmptyCard(kode, nama);
    }

    public boolean cekIsEmptyKode(String kode) throws Exception {
        return this.siswaDAO.cekIsEmptyKode(kode);
    }

    
}
